package com.example.opengldemo.vertex_magical.shader;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES30;
import android.opengl.GLUtils;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

public class WaterRealShader extends LazyRenderer {
    float currStartAngle=0;
    @Override
    protected void onClear() {
    }

    public WaterRealShader(Resources resource, String vert, String frag) {
        super(resource, vert, frag);
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        initVertexData() ;
        shaderHandle.UHandleArray[0] = shaderHandle.getUHandle("uStartAngle");
        shaderHandle.UHandleArray[1] = shaderHandle.getUHandle("uWidthSpan");
        Bitmap bmp = BitmapFactory.decodeResource(mRes, R.drawable.guoqi);
        if (bmp != null) {
            if (markTextureId == -1) {
                markTextureId = TextureUtil.createTextureID(false);
            } else {
                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, markTextureId);
            }
            GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bmp, 0);
            bmp.recycle();
        } else {
            if (markTextureId != -1) {
                GLES30.glDeleteTextures(1, new int[]{markTextureId}, 0);
            }
        }

        new Thread()
        {
            public void run()
            {
                while(true)
                {
                    currStartAngle+=(float) (Math.PI/16);
                    try
                    {
                        Thread.sleep(50);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


    }
    final float WIDTH_SPAN  = 3.3f ;
    public void initVertexData()
    {
        final int cols=12;
        final int rows=cols*3/4;
        final float UNIT_SIZE=WIDTH_SPAN/cols;

        float vertices[]=new float[cols*rows*6*3];
        int count=0;
        for(int j=0;j<rows;j++)
        {
            for(int i=0;i<cols;i++)
            {
                float zsx=-UNIT_SIZE*cols/2+i*UNIT_SIZE;
                float zsy=UNIT_SIZE*rows/2-j*UNIT_SIZE;
                float zsz=0;

                vertices[count++]=zsx;
                vertices[count++]=zsy;
                vertices[count++]=zsz;

                vertices[count++]=zsx;
                vertices[count++]=zsy-UNIT_SIZE;
                vertices[count++]=zsz;

                vertices[count++]=zsx+UNIT_SIZE;
                vertices[count++]=zsy;
                vertices[count++]=zsz;

                vertices[count++]=zsx+UNIT_SIZE;
                vertices[count++]=zsy;
                vertices[count++]=zsz;

                vertices[count++]=zsx;
                vertices[count++]=zsy-UNIT_SIZE;
                vertices[count++]=zsz;

                vertices[count++]=zsx+UNIT_SIZE;
                vertices[count++]=zsy-UNIT_SIZE;
                vertices[count++]=zsz;
            }
        }
        setVertexCo(vertices);
        float texCoor[]=generateTexCoor(cols,rows);
        setTexCoors(texCoor);
    }
    public float[] generateTexCoor(int bw,int bh)
    {
        float[] result=new float[bw*bh*6*2];
        float sizew=1.0f/bw;
        float sizeh=0.75f/bh;
        int c=0;
        for(int i=0;i<bh;i++)
        {
            for(int j=0;j<bw;j++)
            {

                float s=j*sizew;
                float t=i*sizeh;

                result[c++]=s;
                result[c++]=t;

                result[c++]=s;
                result[c++]=t+sizeh;

                result[c++]=s+sizew;
                result[c++]=t;


                result[c++]=s+sizew;
                result[c++]=t;

                result[c++]=s;
                result[c++]=t+sizeh;

                result[c++]=s+sizew;
                result[c++]=t+sizeh;
            }
        }
        return result;
    }

    float ratio  ;
    @Override
    protected void onSizeChanged(int width, int height) {
        super.onSizeChanged(width, height);
        ratio = (float) width / height;
    }

    @Override
    protected void onSetExpandData() {
        super.onSetExpandData();

        //MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 100);
        //MatrixState.setCamera(0,0, 8, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

//        MatrixState.pushMatrix() ;
//        MatrixState.translate(0 , 0 , MatrixState.translate[2] );
//        MatrixState.rotate(MatrixState.rotate[0] , 1 , 0 , 0 );
//        MatrixState.rotate(MatrixState.rotate[1] , 0 , 1 , 0 );
//        shaderHandle.bindMVPMaxtrixHandle(MatrixState.getFinalMatrix(MatrixState.getMMatrix()  , mVertexMatrix )) ;
//        MatrixState.popMatrix() ;

        GLES30.glUniform1f(shaderHandle.UHandleArray[0] , currStartAngle);
        GLES30.glUniform1f(shaderHandle.UHandleArray[1] , WIDTH_SPAN);
    }

//    @Override
//    protected void onDraw() {
//
//        if (markTextureId != -1) {
//            //GLES30.glGetIntegerv(GLES30.GL_VIEWPORT, viewPort, 0);//获取到当前的view窗口大小
//            //设置一个新的大小窗口
//            //GLES30.glViewport(markPort[0],mHeight-markPort[3]-markPort[1],markPort[2],markPort[3]);
//            GLES30.glEnable(GLES30.GL_BLEND);
//            GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
//            GLES30.glBlendEquation(GLES30.GL_FUNC_ADD);
//
//            shaderHandle.bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer()) ;
//            shaderHandle.bindVertexHandle(shaderBuffer.getVertexBuffer()) ;
//            shaderHandle.run(GLES30.GL_TRIANGLES,shaderBuffer.vCount);
//
//            GLES30.glDisable(GLES30.GL_BLEND);
//            //GLES30.glViewport(viewPort[0], viewPort[1], viewPort[2], viewPort[3]);
//        }
//        //todo reset blend and view port
//
//    }
    @Override
    protected void onBindTexture(int textureId) {
        shaderHandle.bindDefaultTextureHandle(markTextureId) ;
    }

    private int markTextureId = -1;


}
